﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery struct
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.
2.
3.
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    ///#region 设置
    var structSetting = {
        css: 'struct'
            , csscontent: 'struct-content'
            , csstools: 'struct-tools'
            , csszoomfit: 'zoomfit'
            , csszoomout: 'zoomout'
            , csszoomin: 'zoomin'
    };
    ///#endregion

    $.fn.struct = function (datas, options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>   
        if (datas) {
            options = $.extend({
                container: iconfig.struct.teml.container
            , item: iconfig.struct.teml.item
            , showtools: true
            , fnselected: null
            , hlinewidth: 4
            , deptforvertical: 2
            }
        , options);
            var struct = $(this), id = struct.attr('id'),structcontent, nodes;;
            structcontent = $('<div />').addClass(structSetting.csscontent).appendTo(struct);
            setStructcontent(struct, structcontent);
            bindTree(id, structcontent, datas, options, 0);

            if (options.showtools)//生成viewer
                struct.viewer({ contentselector: '.' + structSetting.csscontent });

             //structcontent:实现struct的内容区域
            
            //            moveStruct(structcontent);
        }
        //        else {
        //            nodes = struct.children('ul');
        //            if (nodes.length) {
        //                nodes.wrap(document.createElement('div'));
        //                structcontent = nodes.parent().addClass(structSetting.csscontent)
        //            }
        //            else
        //                structcontent = struct.find('div.' + structSetting.csscontent);

        //            if (structcontent.length) {//设置struct的事件
        //                setStructcontent(struct, structcontent, fixheight);
        //                buildStruct(0, true, id, structcontent.children('ul').children('li'), options);
        //            }
        //        }
        return struct;
    };

    ///#region 公共设置方法
    function bindTree(id, root, datas, options, dept) {
        if (datas.length > 0) {
            var container = $(options.container).appendTo(root);
            root.data('count', datas.length);
            ictrcore.teml(container, options.ismulit ? options.itemmulit : options.item, datas).each(function (i) {
                var data = datas[i], item = $(this);
                if (data && data.rows)
                    bindTree(id, item, data.rows, options, dept + 1);
                buildStruct(dept, false, id, item, options);
            });

        }
    }
    function buildStruct(dept, isbuildchild, structid, items, ops) {
        if (items.length > 0) {
            items.each(function () {
                var item = $(this);
                if (!dept)
                    item.addClass('root');

                if (hasValue(ops.deptforvertical) && dept >= ops.deptforvertical)
                    item.addClass('vertical');

                var node = item.children('a'), child = item.children('ul'), childs;
                if (child.length) {
                    //                    node.addClass('dir').click(function () {//文件夹点击时
                    //                                                var node = $(this).toggleClass('opend');
                    //                                                node.next('ul').toggle('fast');
                    //                                                if (fnselected)
                    //                                                    fnselected(node, true);
                    //                    });
                    if (isbuildchild) {
                        childs = child.children('li');
                        buildStruct(dept + 1, isbuildchild, structid, childs, ops);
                    }
                }
                else {//文件点击时
                    //                    node.click(function () {
                    //                                                $('#' + structid + ' a.selected').removeClass('selected');
                    //                                                var node = $(this).addClass('selected');
                    //                                                if (fnselected)
                    //                                                    fnselected(node, false);
                    //                    });

                }
                setStructNode(item, child, childs, ops.hlinewidth);
            });
        }
    }

    function setStructNode(node, child, childs, hlinewidth) {
        if (!childs)
            childs = child.children('li');
        ///#region 设置宽度
        if (childs.length) {
            var width = 0;
            childs.each(function () {
                width += $(this).width();
            });
            if (node.width() < width)
                node.width(width);
        }
        ///#endregion

        ///#region 设置连接线
        var vline = $('<div class="vline"><div /></div>')
            , islast = childs.length == 0, isroot = node.hasClass('root');
        if (islast) {
            if (!isroot)
                node.prepend(vline);
        }
        else {
            var hline = $('<div class="hline"><div/></div>'), first = childs.eq(0), left = first.offset().left + first.width() / 2, linewidth;
            if (childs.length > 1)
                var last = childs.eq(childs.length - 1), linewidth = last.offset().left + last.width() / 2 - left + hlinewidth;

            hline.children('div').width(linewidth || hlinewidth).css('margin-left', left - node.offset().left - hlinewidth / 2);
            if (isroot)
                child.before(vline).before(hline);
            else {
                node.prepend(vline.clone());
                child.prepend(hline).prepend(vline);
            }
        }
        ///#endregion
    }

    function setStructcontent(struct, content) {
        content.css({ width: struct.css('width'), height:struct.css('height')});
    }
    var _ismove, _x, _y;
    function moveStruct(content) {
        if ($.browser.msie) {
            content[0].onselectstart = content[0].ondrag = function () { return false; };
        }

        content.mousedown(function (e) {
            var event = window.event || e;
            _ismove = true;
            _x = event.x || event.clientX;
            _y = event.y || event.clientY;
            $(this).addClass('move');
        });

        content.mousemove(function (e) {
            if (_ismove) {
                var content = $(this);
                content.scrollLeft(content.scrollLeft() - (event.x || event.clientX) + _x);
                content.scrollTop(content.scrollTop() - (event.y || event.clientY) + _y);
            }
        });

        content.bind('mouseup mouseout', function (e) {
            if (_ismove) {
                _ismove = false;
                $(this).removeClass('move');
            }
        });
    }

    ///#endregion

    ///#region 公共方法

    ///#endregion
})(jQuery);